import { Body, Controller, Get, Param, Post, Query, HttpException, HttpStatus, UseFilters, ParseIntPipe, UseGuards } from '@nestjs/common';
import { BookService } from './book.service';
import { HttpExceptionFilter } from 'src/http-exception.filter';
// import { AuthGuard } from 'src/auth.guard';

@Controller('book')
export class BookController {
  constructor(private readonly service: BookService) {}
  @Get()
  index(@Query() query) {
    console.log(query);
    return this.service.index();
  }

  @Get('/ex')
//   @SetMetadata('roles', ['admin'])
  exce() {
    throw new HttpException('出错了', HttpStatus.FORBIDDEN);
  }

  @Get('/ex2')
  exce2() {
    throw new HttpException({
        status: HttpStatus.FORBIDDEN,
        error: '这是一个错误信息'
    }, HttpStatus.FORBIDDEN);
  }

  @Get('/ex3')
  @UseFilters(new HttpExceptionFilter())
  exce3() {
    throw new HttpException({
        status: HttpStatus.FORBIDDEN,
        error: '这是一个错误信息'
    }, HttpStatus.FORBIDDEN);
  }

  @Get(':id')
  findOne(@Param('id', ParseIntPipe) id: number): number {
    console.log(typeof id); // string => nmumber
    return this.service.findOne(id);
  }

  @Post()
  create(@Body() body) {
    return body;
  }


}
